"""doc"""
from uuid import uuid4
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import AbstractUser

from app_base.models import BaseModel

from app_common import app_uuid


# Create your models here.


class WxUser(BaseModel, AbstractUser):
    """doc"""

    class Meta:
        db_table = "WxUser"
        indexes = [
            models.Index(fields=["uuid"]),
            models.Index(fields=["deleted"]),
            models.Index(fields=["created_at"]),
            models.Index(fields=["openid"]),
            models.Index(fields=["nick_name"]),
            models.Index(fields=["telephone"]),
            models.Index(fields=["birthday"]),
            models.Index(fields=["remain_money"]),
            models.Index(fields=["total_recharge"]),
            models.Index(fields=["del_race"]),
        ]

    class UserType(models.TextChoices):
        ANONYMOUS = "游客"
        CUSTOM = "顾客"
        OWNER = "店长"

    # 绑定微信用户
    openid = models.CharField(null=True, max_length=50)

    # 属性
    nick_name = models.CharField(null=True, max_length=50)
    telephone = models.CharField(null=True, max_length=50)
    head_img_url = models.CharField(null=True, max_length=255)
    birthday = models.DateTimeField(null=True)

    # 用户类型
    user_type = models.CharField(
        max_length=20, choices=UserType.choices, default=UserType.ANONYMOUS.value
    )

    # 余额单位，分
    remain_money = models.PositiveBigIntegerField(default=0)
    # 总充值， 方便后续 设置 vip 等级
    total_recharge = models.PositiveBigIntegerField(default=0)


class RechargePromotion(BaseModel):
    """充值活动"""

    class Meta:
        db_table = "RechargePromotion"
        indexes = [
            models.Index(fields=["uuid"]),
            models.Index(fields=["deleted"]),
            models.Index(fields=["created_at"]),
            models.Index(fields=["user_uuid"]),
            models.Index(fields=["total_sale_count"]),
            models.Index(fields=["del_race"]),
        ]

    # 创建人
    user_uuid = models.UUIDField()
    # 实际充值
    actual_payment = models.PositiveBigIntegerField(default=0)
    # 赠送积分
    score = models.PositiveBigIntegerField(default=0)
    limit_count = models.PositiveBigIntegerField(default=9999)
    total_sale_count = models.PositiveBigIntegerField(default=0)


class WxUserRemainMoneyRecord(BaseModel):
    """用户余额变更"""

    class Meta:
        db_table = "WxUserRemainMoneyRecord"
        indexes = [
            models.Index(fields=["uuid"]),
            models.Index(fields=["deleted"]),
            models.Index(fields=["created_at"]),
            models.Index(fields=["del_race"]),
            #
            models.Index(fields=["user_uuid"]),
            # 按照 不同的 类型查看记录
            # 比如
            # 充值就查询, actual_payment != 0
            # 积分赠送就查询, score != 0
            # 消费就查询, consume !=0
            models.Index(fields=["actual_payment"]),
            models.Index(fields=["score"]),
            models.Index(fields=["consume"]),
        ]

    class RechargeType(models.TextChoices):
        """doc"""

        OFF_LINE = "线下充值"
        WEIXIN_PAY = "微信充值"

    user_uuid = models.UUIDField()
    # 变更总余额
    remain_money = models.PositiveBigIntegerField(default=0)

    # 实际充值
    actual_payment = models.PositiveBigIntegerField(default=0)

    # 赠送积分
    score = models.PositiveBigIntegerField(default=0)

    # 花费
    consume = models.PositiveBigIntegerField(default=0)
    shopping_uuid = models.UUIDField(null=True)

    # 充值类型
    recharge_type = models.CharField(
        default=RechargeType.WEIXIN_PAY.value,
        choices=RechargeType.choices,
        max_length=20,
    )


class WxUserPaymentRecord(BaseModel):
    """微信支付记录"""

    class Meta:
        db_table = "WxUserPaymentRecord"
        indexes = [
            models.Index(fields=["uuid"]),
            models.Index(fields=["deleted"]),
            models.Index(fields=["created_at"]),
            models.Index(fields=["user_uuid"]),
            models.Index(fields=["state"]),
            models.Index(fields=["out_trade_no"]),
            models.Index(fields=["openid"]),
            models.Index(fields=["pay_at"]),
        ]

    # base info
    class PaymentState(models.TextChoices):
        """doc"""

        # 待支付, 从 1 开始
        WAIT = "未支付"
        # 支付成功
        SUCCESS = "成功"
        # 支付成功
        FAIL = "失败"
        # 已经退款
        REFUND = "已退款"

    class PayFor(models.TextChoices):
        """支付类型"""

        PROMOTION = "活动"
        SHOPPING = "购物"

    # 充值订单
    user_uuid = models.UUIDField()

    # 订单uuid
    # out_trade_no”映射到值字段“商户订单号”字符串规则校验失败，字节数 36，大于最大值 32
    out_trade_no = models.UUIDField(default=app_uuid.hex_uuid, editable=False)
    spbill_create_ip = models.CharField(null=True, max_length=50)
    mch_id = models.CharField(null=True, max_length=50)
    env_id = models.CharField(null=True, max_length=50)
    service = models.CharField(null=True, max_length=50)

    # 金额单位：分
    total_fee = models.PositiveIntegerField()
    # 赠送积分：分
    score = models.PositiveIntegerField()

    openid = models.CharField(max_length=50)
    # 参考 OrderState
    state = models.CharField(
        default=PaymentState.WAIT.value, choices=PaymentState.choices, max_length=20
    )
    pay_for = models.CharField(choices=PayFor.choices, max_length=20)

    pay_at = models.DateTimeField(auto_now=True)

    # 账单详情
    order_title = models.CharField(max_length=255)


class StoreGoodType(BaseModel):
    """商品种类"""

    class Meta:
        db_table = "StoreGoodType"
        indexes = [
            models.Index(fields=["uuid"]),
            models.Index(fields=["deleted"]),
            models.Index(fields=["created_at"]),
            models.Index(fields=["del_race"]),
            #
            models.Index(fields=["name"]),
            models.Index(fields=["weight"]),
        ]
        unique_together = [["del_race", "name"]]

    name = models.CharField(max_length=50)

    # 排序权重越高越靠前
    weight = models.PositiveBigIntegerField(null=False)


# good
class StoreGood(BaseModel):
    """商品"""

    class Meta:
        db_table = "StoreGood"
        indexes = [
            models.Index(fields=["uuid"]),
            models.Index(fields=["deleted"]),
            models.Index(fields=["created_at"]),
            models.Index(fields=["del_race"]),
            #
            models.Index(fields=["owner_uuid"]),
            models.Index(fields=["price"]),
            models.Index(fields=["name"]),
            models.Index(fields=["state"]),
            models.Index(fields=["total_sale_count"]),
        ]
        unique_together = [["del_race", "name"]]

    class GoodState(models.TextChoices):
        """doc"""

        ONSALE = "在售"
        SOLD_OUT = "售空"
        OFF_SHELF = "下架"

    # 创建人
    owner_uuid = models.UUIDField()

    # 账单详情
    type_uuid = models.UUIDField()
    name = models.CharField(max_length=50)
    display_url = models.CharField(max_length=255)
    state = models.CharField(
        default=GoodState.ONSALE.value, choices=GoodState.choices, max_length=20
    )
    # 进价
    purchase_price = models.PositiveBigIntegerField(null=False)

    # 商品单价
    price = models.PositiveBigIntegerField(null=False)

    # 剩余数量
    limit_count = models.PositiveBigIntegerField(default=9999)
    total_sale_count = models.PositiveBigIntegerField(default=0)


class StoreGoodEvaluation(BaseModel):
    """商品评价"""

    class Meta:
        db_table = "StoreGoodEvaluation"
        indexes = [
            models.Index(fields=["uuid"]),
            models.Index(fields=["deleted"]),
            models.Index(fields=["created_at"]),
            #
            models.Index(fields=["user_uuid"]),
        ]
        unique_together = [["del_race", "user_uuid", "good_uuid"]]

    # 评价者
    user_uuid = models.UUIDField()
    # 被评价商品
    good_uuid = models.UUIDField()
    display_url = models.CharField(max_length=255)
    detail = models.CharField(max_length=255)


# 购物车 内容
class StoreGoodCarContent(BaseModel):
    """购物车内容"""

    class Meta:
        db_table = "StoreGoodCarContent"
        indexes = [
            models.Index(fields=["uuid"]),
            models.Index(fields=["deleted"]),
            models.Index(fields=["created_at"]),
            #
            models.Index(fields=["user_uuid"]),
            models.Index(fields=["good_uuid"]),
        ]

    # 购物车 关联者
    user_uuid = models.UUIDField()
    good_uuid = models.UUIDField()
    pay_count = models.PositiveBigIntegerField(default=1)


class StoreShopping(BaseModel):
    """用户余额变更"""

    class Meta:
        db_table = "StoreShopping"
        indexes = [
            models.Index(fields=["uuid"]),
            models.Index(fields=["deleted"]),
            models.Index(fields=["created_at"]),
            #
            models.Index(fields=["user_uuid"]),
        ]

    class ShoppingState(models.TextChoices):
        """doc"""

        WAIT = "待支付"
        PAID = "已付"
        REFUND = "退款"

    class PayType(models.TextChoices):
        """doc"""

        REMAIN = "余额"
        WEIXIN = "微信"
        MIX = "混合"

    # 购物记录 关联者
    user_uuid = models.UUIDField()

    #
    payment_record_uuid = models.UUIDField(null=True)

    #
    state = models.CharField(
        default=ShoppingState.WAIT.value, choices=ShoppingState.choices, max_length=20
    )

    pay_type = models.CharField(choices=PayType.choices, max_length=20)
    # 原价 与 折后价格
    original_price = models.PositiveBigIntegerField()
    actual_payment = models.PositiveBigIntegerField()

    # 不同支付方式支付的金额
    pay_remain_money = models.PositiveBigIntegerField()
    pay_weixin = models.PositiveBigIntegerField()


class StoreShoppingContent(BaseModel):
    """用户余额变更"""

    class Meta:
        db_table = "StoreShoppingContent"
        indexes = [
            models.Index(fields=["uuid"]),
            models.Index(fields=["deleted"]),
            models.Index(fields=["created_at"]),
            #
            models.Index(fields=["user_uuid"]),
            models.Index(fields=["shopping_uuid"]),
            models.Index(fields=["good_uuid"]),
        ]

    # 购物记录 关联者
    user_uuid = models.UUIDField()
    shopping_uuid = models.UUIDField()
    good_uuid = models.UUIDField()
    # 购买数量
    pay_count = models.PositiveBigIntegerField()
    # 单价
    original_price = models.PositiveBigIntegerField()
    # 总原价
    sum_original_price = models.PositiveBigIntegerField()
    # 实际价格
    sum_actual_payment = models.PositiveBigIntegerField()
